package com.fr.web.core.service;

import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.file.ConfigManager;
import com.fr.report.web.ui.IconManager;
import com.fr.report.web.ui.WidgetManager;
import com.fr.web.Browser;
import com.fr.web.core.WebUtils;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EmbResourceService extends NoSessionIDOPService
{
  public static final String DEFAULT_JS_NAME = "finereport.js";
  public static final String TOOLBAR_CSS_NAME = "toolbar.css";
  public static final String DEFAULT_CSS_NAME = "finereport.css";
  private static String defaultJs = null;
  private static String defaultCss = null;
  private static EmbResourceService EMBRESOURCE_SERVICE = new EmbResourceService();

  public static EmbResourceService getInstance()
  {
    return EMBRESOURCE_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.equals("emb");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    dealWithEmbResource(paramHttpServletRequest, paramHttpServletResponse);
  }

  public static void dealWithEmbResource(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Object localObject;
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "resource");
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    paramHttpServletResponse.setHeader("Cache-Control", "max-age=86400");
    paramHttpServletResponse.setDateHeader("Expires", System.currentTimeMillis() + 86400000L);
    String str2 = FRContext.getConfigManager().getServerCharset();
    if ("finereport.js".equals(str1))
    {
      paramHttpServletResponse.setContentType("text/javascript;charset=" + str2);
      localObject = WebUtils.createTemplateMap(paramHttpServletRequest);
      TemplateUtils.dealWithTemplate(new StringReader(createDefaultJs()), localPrintWriter, (Map)localObject);
    }
    else if ("finereport.css".equals(str1))
    {
      paramHttpServletResponse.setContentType("text/css;charset=" + str2);
      localObject = WebUtils.createTemplateMap(paramHttpServletRequest);
      TemplateUtils.dealWithTemplate(new StringReader(createDefaultCss()), localPrintWriter, (Map)localObject);
    }
    else if ("toolbar.css".equals(str1))
    {
      localObject = createToolbarCss(paramHttpServletRequest);
      paramHttpServletResponse.setContentType("text/css;charset=" + str2);
      if (localObject == null)
        localObject = "";
      localPrintWriter.write((String)localObject);
    }
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  public static String createDefaultJs()
    throws Exception
  {
    if (defaultJs != null)
      return defaultJs;
    String[] arrayOfString = { "/com/fr/web/jquery.js", "/com/fr/web/load.js", "/com/fr/web/core/js/i18n.js", "/com/fr/web/core/js/events.js", "/com/fr/web/core/js/jquery.hotkeys.js", "/com/fr/web/core/js/ui/jquery.ui.core.js", "/com/fr/web/core/js/ui/jquery.ui.widget.js", "/com/fr/web/core/js/ui/jquery.ui.mouse.js", "/com/fr/web/core/js/ui/jquery.ui.position.js", "/com/fr/web/core/js/ui/jquery.ui.draggable.js", "/com/fr/web/core/js/ui/jquery.ui.resizable.js", "/com/fr/web/core/js/ui/jquery.ui.button.js", "/com/fr/web/core/js/ui/jquery.ui.dialog.js", "/com/fr/web/core/js/ui/jquery.ui.tabs.js", "/com/fr/web/core/js/jLayout.js", "/com/fr/web/core/js/jquery.sizes.js", "/com/fr/web/core/js/jquery.jlayout.js", "/com/fr/web/core/js/jquery.menu.js", "/com/fr/web/core/js/jquery.ux.js", "/com/fr/web/core/js/jquery.ux.date.js", "/com/fr/web/core/js/form/fr.form.panel.js", "/com/fr/web/core/js/form/fr.form.layout.border.js", "/com/fr/web/core/js/form/fr.form.tabs.js" };
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < arrayOfString.length; ++i)
    {
      localStringBuffer.append(BaseUtils.readResourceAsString(arrayOfString[i]));
      if (i < arrayOfString.length - 1)
        localStringBuffer.append(";");
    }
    defaultJs = localStringBuffer.toString();
    return defaultJs;
  }

  public static void setDefaultJS(String paramString)
  {
    defaultJs = paramString;
  }

  private static String createDefaultCss()
    throws Exception
  {
    if (defaultCss != null)
      return defaultCss;
    String[] arrayOfString = { "/com/fr/web/core/css/page.css", "/com/fr/web/core/css/redmond/jquery-ui.custom.css", "/com/fr/web/core/css/widget/fr.form.panel.css", "/com/fr/web/core/css/widget/fr.form.tabs.css", "/com/fr/web/core/css/widget/fr.form.borderlayout.css", "/com/fr/web/core/css/widget/fr.form.widget.css", "/com/fr/web/core/css/jquery.menu.css", "/com/fr/web/core/css/jquery.ux.css" };
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < arrayOfString.length; ++i)
    {
      localStringBuffer.append(BaseUtils.readResourceAsString(arrayOfString[i]));
      if (i < arrayOfString.length - 1)
        localStringBuffer.append("\n");
    }
    defaultCss = localStringBuffer.toString();
    return defaultCss;
  }

  private static String createToolbarCss(HttpServletRequest paramHttpServletRequest)
  {
    return FRContext.getWidgetManager().getIconManager().getCssFile(WebUtils.createServletURL(paramHttpServletRequest), Browser.resolve(paramHttpServletRequest));
  }
}